<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #ball{
            width: 100px;
            height: 100px;
            background-color: red;
            border-radius:50%;
            margin-left: 0px;
        }
        #ball2{
            width: 100px;
            height: 100px;
            background-color: red;
            border-radius:50%;
            margin-left: 0px;
        }
    </style>
</head>
<body>
<div id="ball"></div>
<input type="button" value="播放" id="btn1" >
<input type="button" value="暂停" id="btn2" >
<script>


    var btn1 = document.getElementById('btn1');
    var btn2 = document.getElementById('btn2');



    //找到小球原始的 marginLeft位置
    //1找元素的对象
    var ball = document.getElementById('ball');

    function go(fps){//frame per second
        var t;//闭包
        function myMove(id,end){
            var htmlObj = document.getElementById(id);
            clearInterval(t);
            var offset = 1;     //速度
            function move(){
                var cssObj = getComputedStyle(htmlObj);
                var originL = parseInt(cssObj.marginLeft);
                if (originL>=end){
                    clearInterval(t);
                } else {
                    //2 根据找到的对象，再找这个对象的 css相关信息

                    //新的位置 = 原始位置+新的偏移量

                    htmlObj.style.marginLeft = originL + offset + 'px';
                }

            }

            var m = 1000/fps; //
            t = setInterval(move,m);//1
        }
        function stop(){
            clearInterval(t);
        }
        return {
            go:myMove,
            stop:stop
        };
    }

var move1 = go(50);
btn1.onclick = function(){
    move1.go('ball',500);//var fun = move[0],fun('ball',500)
};
btn2.onclick = function(){
    move1.stop();//var fun = move1[1],fun()
};






</script>

</body>
</html>